package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.common.utils.math.RandomUtils;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.ChessBoard;
import com.sghd.fight.module.game.core.Grid;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.*;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author frank
 * @date 2018-12-13 14:49
 * @desc 吞噬
 **/
@Component
public class PhagocytosisEffect implements Effect {

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        ChessType chessType = null;
        do {
            chessType = ChessType.values()[RandomUtils.nextInt(ChessType.values().length)];
        } while (chessType == ChessType.CT_LIGHTNING);
        skillReport.getIndexs().add(chessType.ordinal());
        ChessBoard chessBoard = room.getChessBoard();
        Grid[] grids = chessBoard.getGrids();
        Set<Integer> elimited = new HashSet<>();
        for (Grid grid : grids) {
            if (grid.getType() == chessType) {
                elimited.add(grid.getIndex());
                owner.getMagic().collect(grid.getType());//收集
                grid.remove();//消除
            }
        }
        int level = owner.getSkillLevel(config.getId());
        Map<String, Object> attack2Level = MapUtils.getMap(ctx, String.valueOf(level));
        int attack = MapUtils.getIntValue(attack2Level, ATTACK);
        if(attack != 0){
            attack = (int) (attack * getBet(owner, target));
            attack = fixHp(owner, target, attack, room, config);
            context.addValue(target, UnitValue.HP, -attack);//扣血
            targetReport.addValue(ValueItem.valueOf(UnitValue.HP, -attack));
        }
        chessBoard.getEliminated().addAll(elimited);
        chessBoard.fill();
        //自动消除
        autoElimi(room, owner, skillReport, chessBoard);

    }

    @Override
    public EffectType getType() {
        return EffectType.SK_PHAGOCYTOSIS;
    }
}
